This invention relates generally to object oriented programming. More particularly, it relates to an improved method for object initialization.
A well known technology is object oriented programming which encapsulates data and methods into a programming entity called an object. By protecting certain methods and data through a public interface, an object oriented program can insulate each component from changes to other components yet provide the needed functions with a minimum of reprogramming. For background information on object oriented technologies, concepts and conventions, the reader is referred to references such as Object Oriented Design With Applications, Grady Booch (The Benjamin/Cummins Publishing Company, 1990) and Object Oriented Software Construction, by B. Meyer, (Prentice Hall, 1988).
In class-based object oriented programming models, the content of an object includes of the instance variables which are introduced by the class of the object in addition to the instance variables introduced by the ancestor classes of the object's class. Before an object is made generally available to users, these instance variables must be placed into a consistent state. This process is called object initialization and it is accomplished by allowing each class to define special code which is executed when an object belonging to the class is initialized.
In the prior art, there are basically two different approaches to object initialization: initialization code may be placed with constructors, e.g., in the C++ model, or within normal methods.
In the C++ constructor model, a class can define any number of different "constructors" that have different argument lists. The information specified by a constructor definition concerns how the instance variables introduced by the constructor's class are to be initialized which depend on the constructor arguments as well as which constructors should be called for ancestor classes and what arguments should be passed to these constructors. The code that creates a C++ object statically indicates the class of the object and the desired constructor to be used to initialize the class's introduced data, and passes the necessary arguments. By "statically," it is meant that the code explicitly indicates information in a way that does not depend on the runtime content of data variables. Because each constructor statically determines which ancestor constructors are used, and what their arguments are the overall result of object creation is to create a chain of constructor invocations which begins with the class of the object and continues through each of its ancestor classes. This allows the class of an object, and each of the ancestors of this class, the opportunity of initializing the data the class introduces within an object. One general reference on C++ is The C++ Progranming Language (Second Edition, Addison-Wesley, 1993) by Bjarne Stroustrup of Bell Laboratories, the author of C++.
The main advantage of the C++ approach is that each class gets to provide multiple constructors based on the instance variables introduced by that class, and subclasses and general users, i.e. applications which use the class, get to choose from these multiple constructors, based on the information available to the general users. Also, the approach handles general multiple inheritance hierarchies with "diamond tops." A diamond top is a class which has two or more subclasses with a common descendant. One problem with diamond tops is to avoid executing their initialization code more than once. Drawbacks of the constructor approach include the following: initialization is directly associated with object creation, i.e. objects that already exist cannot be re-initialized, the code that initializes an object must know the exact class of the object, and the ancestor constructors that are called from a constructor cannot be selected based on the runtime values of arguments passed to the constructor.
An alternative model for initialization is to use a "normal" method for this purpose. Smalltalk is an example of an object oriented programming system which uses a "normal" method for initialization. The reader is referred to Smalltalk-80 by Adele Goldberg (Addison-Wesley, 1983) for further information on Smalltalk. Each subclass below the introducing class then overrides this initialization method with an implementation which initializes the instance variables that it introduces, and then, makes a "parent method call" to allow ancestor classes the same opportunity. Again, a chain including all necessary ancestors is constructed. One term for this approach is a method-based model with overrides.
Advantages of this method-based approach include the fact that it is not necessary to create an object to initialize it. Thus, objects can be reinitialized. Nor is it necessary to know the exact class of the object. The two main disadvantages of this approach are the inflexibility of a single initialization method with a single set of arguments and the fact that the method-based approach does not generalize to multiple inheritance class hierarchies with diamond tops.
The description below describes a new approach to object initialization that combines the strengths of both techniques and avoids their disadvantages.